//
//  NormalModel.swift
//  MeasureMould
//
//  Created by iOS on 2021/3/31.
//

import Foundation
import MapKit

class NormalModel: NSObject {
    /// 1字节 协议开始字符，取值固定为 “$”
    var pro_leadChar:UInt8 = 0
    var pro_gps_power:UInt8 = 0
    var pro_bytes:UInt8 = 0
    ///经度
    var pro_longitude:Double = 0
    ///纬度
    var pro_latitude:Double = 0
    var pro_num:UInt8 = 0
    var pro_mode:UInt8 = 0
    var pro_hdop:UInt8 = 0
    var pro_speed:UInt8 = 0
    var pro_altitude:[UInt8] = [0x00, 0x00]
    var pro_voltage:[UInt8] = [0x00, 0x00]
    var pro_crc:[UInt8] = [0x00, 0x00]
    var pro_endChar:UInt = 0

    var lat_d:Double = 0
    var lat_m:Double = 0
    var lat_s:Double = 0

    var lon_d:Double = 0
    var lon_m:Double = 0
    var lon_s:Double = 0

    func read_analyzeData(formData data:Data){
        let byteArr = data
        if byteArr.count > 0 {
            for (index,value) in byteArr.enumerated() {
                switch index {
                    case 0:
                        break
                    default:
                        break
                }
            }
            //103 56 4846 20
//            let long_d = Double(byteArr[3])
//            let long_m = Double(byteArr[4]) / 60
//            let data_lon = UInt(byteArr[5])
//            let temp_lon = data_lon << 8
//            let long_s = Double(temp_lon) /// 60 / 60
//            let long_s1 = Double(byteArr[6]) /// 60 / 60 / 10
//            let long_s2 = Double(byteArr[7]) /// 60 / 60 / 100
////            let lon_sss = Double(String("\(byteArr[5]).\(byteArr[6])\(byteArr[7])"))!/3600
//            let sss = (long_s + long_s1 + long_s2) / 3600 / 100
//            let long = long_d + long_m + sss //long_s + long_s1 + long_s2
            //print("long_s == \(byteArr[5] << 8) \n long_s1 == \(long_s1) \n long_s2 == \(long_s2)")

//            let lat_d = Double(byteArr[8])
//            let lat_m = Double(byteArr[9]) / 60
//            let data:UInt32 = UInt32(byteArr[10])
//            let lat_sss = data<<8
//            let lat_s = Double(lat_sss) /// 60 / 60
//            let lat_s1 = Double(byteArr[11]) /// 60 / 60 / 10
//            let lat_s2 = Double(byteArr[12]) /// 60 / 60 / 100
//             let ss_at = (lat_s + lat_s1 + lat_s2) / 3600 / 100
//            let lat = lat_d + lat_m + ss_at//lat_s + lat_s1 + lat_s2

            
            let str_lat = String(format: "%d°", byteArr[8]) + String(format: "%d′", byteArr[9]) + String(format: "%d.%d%d″", byteArr[10], byteArr[11], byteArr[12])
            let d_lat = changeToDu(dms: str_lat)

            let str_lon = String(format: "%d°", byteArr[3]) + String(format: "%d′", byteArr[4]) + String(format: "%d.%d%d″", byteArr[5], byteArr[6], byteArr[7])
            let d_lon = changeToDu(dms: str_lon)

//            self.lon_d = long_d
//            self.lon_m = Double(byteArr[4])
//            self.lon_s = (long_s / 100 + long_s1 / 1000 + long_s2 / 1000)
//
//            self.lat_d = lat_d
//            self.lat_m = Double(byteArr[9])
//            self.lat_s = (lat_s / 100 + lat_s1 / 1000 + lat_s2 / 1000)

            let leadChar = byteArr[0]
            let gps_power = byteArr[1]
            let bytes = byteArr[2]
//            let longitude = String(format: "\(byteArr[3]).\(byteArr[4])\(byteArr[5])\(byteArr[6])\(byteArr[7])")
//            let latitude = String(format: "\(byteArr[8]).\(byteArr[9])\(byteArr[10])\(byteArr[11])\(byteArr[12])")
            self.pro_leadChar = leadChar
            self.pro_gps_power = gps_power
            self.pro_bytes = bytes
//            let coord2D = CLLocationCoordinate2D(latitude: d_lat, longitude: d_lon)
//            let amapcoord = AMapCoordinateConvert(coord2D, AMapCoordinateType.GPS)
//            self.pro_longitude = amapcoord.longitude
//            self.pro_latitude = amapcoord.latitude

            self.pro_speed = byteArr[16] + byteArr[17]

        }

    }


    func boolthDataWithHex(_ hex:String){
        let oc_hex = hex as NSString
        let lon_d = oc_hex.substring(with: NSRange(location: 6, length: 2))
        let dd = Int(lon_d, radix: 16)!
        let lon_m = oc_hex.substring(with: NSRange(location: 8, length: 2))
        let mm = Int(lon_m, radix: 16)!
        let lon_s = oc_hex.substring(with: NSRange(location: 10, length: 4))
        let s = Int(lon_s, radix: 16)!
        let lon_ss = oc_hex.substring(with: NSRange(location: 14, length: 2))
        let ss = Int(lon_ss, radix: 16)!

        let lat_d = oc_hex.substring(with: NSRange(location: 16, length: 2))
        let dd_lat = Int(lat_d, radix: 16)!
        let lat_m = oc_hex.substring(with: NSRange(location: 18, length: 2))
        let mm_lat = Int(lat_m, radix: 16)!
        let lat_s = oc_hex.substring(with: NSRange(location: 20, length: 4))
        let s_lat = Int(lat_s, radix: 16)!
        let lat_ss = oc_hex.substring(with: NSRange(location: 24, length: 2))
        let ss_lat = Int(lat_ss, radix: 16)!

        let lon = LocationConverter.getLatLon(dd, mm: mm, ss: s, sss: ss)
        let lat = LocationConverter.getLatLon(dd_lat, mm: mm_lat, ss: s_lat, sss: ss_lat)
//        self.pro_latitude = lat
//        self.pro_longitude = lon
        let coord2D = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        let amapcoord = AMapCoordinateConvert(coord2D, AMapCoordinateType.GPS)
        self.pro_longitude = amapcoord.longitude
        self.pro_latitude = amapcoord.latitude

    }

    


}
